/**
 * 
 */
package com.kkks.spring_rest.tick.component.impl;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.collections.map.ListOrderedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.kkks.spring_rest.tick.component.UserComponent;
import com.kkks.spring_rest.tick.dao.HibernateTemplateBaseDao;
import com.kkks.spring_rest.tick.model.WhoUserBaseInfo;
import com.kkks.spring_rest.tick.model.WhoUsers;
import com.kkks.spring_rest.tick.utils.ConvertHelper;
import com.kkks.spring_rest.tick.utils.DateHelper;
import com.kkks.spring_rest.tick.utils.PasswordParseHelper;
import com.kkks.spring_rest.tick.utils.ThreeDESHelper;

/**
 * @author SC
 * @version 1.0
 * @created 2012-7-17 上午10:52:56
 * @describe 用户管理容器
 */

@Component("userComponent")
@Transactional
@SuppressWarnings("rawtypes")
public class UserComponentImpl implements UserComponent {

	@Autowired
	private HibernateTemplateBaseDao hibernateTemplateBaseDao;

	public HibernateTemplateBaseDao getHibernateTemplateBaseDao() {
		return hibernateTemplateBaseDao;
	}

	public void setHibernateTemplateBaseDao(
			HibernateTemplateBaseDao hibernateTemplateBaseDao) {
		this.hibernateTemplateBaseDao = hibernateTemplateBaseDao;
	}

	/**
	 * 验证用户Id 以及 用户编码
	 * 
	 * @param userId
	 * @param userToken
	 * @return boolean
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("unchecked")
	public boolean validUserToken(String userId, String userToken) {
		boolean flag = false;
		try {
			WhoUsers whoUsers = (WhoUsers) this.hibernateTemplateBaseDao
					.findById(WhoUsers.class, Integer.parseInt(userId));
			if (whoUsers != null) {
				String origion = ThreeDESHelper.decryptMode(java.net.URLDecoder
						.decode(userToken, "utf-8"));
				String userNameandId = whoUsers.getUserName()
						+ whoUsers.getUserId();
				if (origion.equals(userNameandId)) {
					flag = true;
				} else {
					flag = false;
				}
			} else {
				flag = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 用户注册
	 * 
	 * @param userName
	 * @param password
	 * @param confirm
	 * @param phoneType
	 * @return ListOrderedMap
	 */
	@SuppressWarnings("unchecked")
	public ListOrderedMap userRegister(String userName, String password,
			String confirm, String phoneType) {
		ListOrderedMap map = new ListOrderedMap();

		try {
			if (!password.equals(confirm)) {
				map.put("resultValue", 0);
				map.put("errorDescription", "密码输入不一致");
			} else {
				if (isExistUserNameEmail(userName)) {
					map.put("resultValue", 0);
					map.put("errorDescription", "用户名已存在!");
				} else {

					// 构建WhoUsers实体对象
					WhoUsers user = new WhoUsers();
					user.setUserName(userName);
					user.setEmail(userName);
					user.setAvatar("");
					user.setQuestion("");
					user.setAnswer("");
					user.setSex(true);
					user.setBirthday(DateHelper.getCurrentDate());
					user.setAddressId(0);
					user.setUserRank((short) 1);
					user.setParentId(0);
					user.setAlias("");
					user.setMsn("");
					user.setOfficePhone("");
					user.setHomePhone("");
					user.setMobilePhone("");
					user.setIsValidated((short) 0);
					user.setCreditLine((double) 0);
					user.setYahoo("");
					user.setSkype("");
					user.setCountry("");
					user.setIsGoogle(0);
					if (phoneType != null) {
						if (phoneType.equals("10")) {
							user.setIsGoogle(10);
						} else if (phoneType.equals("11")) {
							user.setIsGoogle(11);
						} else if (phoneType.equals("12")) {
							user.setIsGoogle(12);
						}
					} else {
						user.setIsGoogle(0);
					}
					user.setAvatar("avatar");
					user.setGoodsComment((short) 0);

					WhoUserBaseInfo userBaseInfo = new WhoUserBaseInfo();
					userBaseInfo.setEmail(userName);
					userBaseInfo.setIsVip(true);
					userBaseInfo.setLastIp((long) 0);
					userBaseInfo.setLastLogin(0);
					userBaseInfo.setLastPaymentId(0);
					userBaseInfo.setLastShippingId(0);
					userBaseInfo.setManagerId(0);
					userBaseInfo.setPassword(PasswordParseHelper
							.getEncryptPassword(password));
					userBaseInfo.setPayPoints(0);
					userBaseInfo.setRankPoints(0);
					userBaseInfo.setRegIp((long) 0);
					userBaseInfo.setRegTime(ConvertHelper.DateToInt(DateHelper
							.getCurrentDate()));
					userBaseInfo.setUserBalance((double) 0.0);
					userBaseInfo.setUserName(userName);
					userBaseInfo.setUserRank(false);
					userBaseInfo.setVisitCount(0);

					user.setWhoUserBaseInfo(userBaseInfo);
					this.hibernateTemplateBaseDao.save(user);
					map.put("resultValue", 1);
					map.put("errorDescription", "注册成功");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			new Throwable("用户注册失败");
			map.put("resultValue", 0);
			map.put("errorDescription", "网络忙碌");
		}

		return map;
	}

	/**
	 * 判断用户名Email是否存在
	 * 
	 * @param userName
	 *            用户名Email
	 * @return 存在true， 不存在false
	 */
	public boolean isExistUserNameEmail(String userName) {
		boolean result = false;
		try {
			List userList = this.hibernateTemplateBaseDao.findListByHql(
					" from WhoUsers where userName = ? or email = ?",
					new Object[] { userName, userName });

			if (userList.size() > 0) {
				result = true;
			} else {
				result = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
